package com.zhengqing.softdemo.generate

//class CodeGen {
//}

import com.mybatisflex.codegen.Generator
import com.mybatisflex.codegen.config.ColumnConfig
import com.mybatisflex.codegen.config.GlobalConfig
import com.zaxxer.hikari.HikariDataSource

object CodeGen {
    @JvmStatic
    fun main(args: Array<String>) {
        //配置数据源
        val dataSource = HikariDataSource()
        dataSource.jdbcUrl = "jdbc:mysql://localhost:3306/web?characterEncoding=utf-8"
        dataSource.username = "root"
        dataSource.password = "123456"

        // 只生成 Entity 和 TableDef，表更新时用！
        // val globalConfig = genSoftDefEntity()

        // val globalConfig = createTableDef();

        //创建配置内容，两种风格都可以。
        // globalConfig = createGlobalConfigUseStyleOne()
        val globalConfig = createGlobalConfigUseStyleOne()
        // val globalConfig = createGlobalConfigUseStyleTwo();

        //通过 datasource 和 globalConfig 创建代码生成器
        val generator = Generator(dataSource, globalConfig)

        //生成代码
        generator.generate()
    }

    fun genSoftDefEntity(): GlobalConfig {
        // 不会覆盖！表结构变了，选哟删除再生成
        val globalConfig = GlobalConfig()
        globalConfig.basePackage = "com.example.demo"
        globalConfig.setTablePrefix("tb_")
        globalConfig.setGenerateTable("tb_soft")
        globalConfig.isEntityGenerateEnable = true
        globalConfig.isEntityWithLombok = true
        globalConfig.isTableDefGenerateEnable = true
        return globalConfig
    }

    fun createTableDef(): GlobalConfig {
        //创建配置内容
        val globalConfig = GlobalConfig()

        //设置根包
        globalConfig.basePackage = "com.example.demo"
        globalConfig.setTablePrefix("tb_")
        globalConfig.setGenerateTable("tb_order_good")
        globalConfig.setGenerateTable("tb_account_order")

        //设置生成 entity 并启用 Lombok
        globalConfig.isEntityGenerateEnable = true
        globalConfig.isEntityWithLombok = true
        globalConfig.isTableDefGenerateEnable = true
        return globalConfig
    }

    fun createGlobalConfigUseStyleOne(): GlobalConfig {
        //创建配置内容
        val globalConfig = GlobalConfig()

        //设置根包
        globalConfig.basePackage = "com.example.demo"

        //设置表前缀和只生成哪些表
        globalConfig.setTablePrefix("tb_")
        globalConfig.setGenerateTable("tb_soft")

        //设置生成 entity 并启用 Lombok
        globalConfig.isEntityGenerateEnable = true
        globalConfig.isEntityWithLombok = true

        //设置生成 mapper
        globalConfig.isMapperGenerateEnable = true
        globalConfig.isServiceGenerateEnable = true
        globalConfig.isServiceImplGenerateEnable = true
        globalConfig.isControllerGenerateEnable = true
        globalConfig.isTableDefGenerateEnable = true
        globalConfig.isMapperXmlGenerateEnable = true

        //可以单独配置某个列 // 这样设置id查出来是空的！！
        val columnConfig = ColumnConfig()
        columnConfig.columnName = "id"
        columnConfig.large = true
        columnConfig.version = true
        globalConfig.setColumnConfig("tb_soft", columnConfig)
        return globalConfig
    }

    fun createGlobalConfigUseStyleTwo(): GlobalConfig {
        //创建配置内容
        val globalConfig = GlobalConfig()

        //设置根包
        globalConfig.packageConfig.basePackage = "com.example.demo"

        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.strategyConfig
                .setTablePrefix("tb_")
                .setGenerateTable("tb_soft")

        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity().isWithLombok = true

        //设置生成 mapper
        globalConfig.enableMapper()

        // 设置生成 service 设置生成 ServiceImpl
        globalConfig.enableService()
        globalConfig.enableServiceImpl()

        // 设置生成 Controller
        globalConfig.enableController()

        // 设置生成 TableDef
        globalConfig.enableTableDef()

        // 设置生成 MapperXml
        globalConfig.enableMapperXml()


        //可以单独配置某个列
        val columnConfig = ColumnConfig()
        columnConfig.columnName = "soft_id"
        columnConfig.large = true
        columnConfig.version = true
        globalConfig.strategyConfig
                .setColumnConfig("tb_soft", columnConfig)
        return globalConfig
    }
}